package com.jihox.abookpro.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jihox.abookpro.R;

public class AddAndSubText extends LinearLayout {

	Button mADD, mSUB;
	EditText valueText;
	int MAX = 100;
	int MIN = 1;
	int value = MIN;
	private TextWatcher valueWatcher;

	public AddAndSubText(Context context) {
		super(context);
	}

	public AddAndSubText(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.add_sub_text, this);
		init();// 初始化
	}

	/**
	 * 初始化，设置初始值
	 */
	private void init() {
		mADD = (Button) findViewById(R.id.add);
		mSUB = (Button) findViewById(R.id.sub);
		this.mADD.setOnClickListener(listener);
		this.mSUB.setOnClickListener(listener);
		mSUB.setEnabled(false);
		valueText = (EditText) findViewById(R.id.value);
		this.valueText.setText(value + "");// 设置初始值为1
		valueText
				.setFilters(new InputFilter[] { new NumberInputFilter(MIN, MAX)

				});
		valueText.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable view) {
				// TODO Auto-generated method stub
				// 改变value的值
				if (valueText.getText() == null
						|| valueText.getText().toString().equals("")) {
					value = 1;
					return;
				} else {
					value = setCheckadle(Integer.parseInt(valueText.getText()
							.toString()));
				}
				// valueText.setText(value+"");
				if (valueWatcher != null) {
					valueWatcher.afterTextChanged(view);
				}
				return;
			}

			@Override
			public void beforeTextChanged(CharSequence text, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}

		});
	}

	/**
	 * 加减号事件响应
	 */
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.add:
				value++;
				value = setCheckadle(value);
				valueText.setText(value + "");
				break;
			case R.id.sub:
				value--;
				value = setCheckadle(value);
				valueText.setText(value + "");
				break;
			}
		}
	};

	/**
	 * 判断num值，对加减号进行设置：若num = 0，；若num = 99，（限制num在0~99区间）
	 * 
	 * @param num
	 * @return
	 */
	private int setCheckadle(int num) {
		if (num <= MIN) {
			num = MIN;
			this.mSUB.setEnabled(false);
		} else if (num >= MAX) {
			num = MAX;
			this.mADD.setEnabled(false);
		} else {
			this.mSUB.setEnabled(true);
			this.mADD.setEnabled(true);
		}
		return num;
	}

	/**
	 * 重写toString() 返回value值
	 */
	@Override
	public String toString() {
		return String.valueOf(value);
	}

	/**
	 * 获得value 的int 值
	 */
	public int getValue() {
		return value;
	}

	public void addTextChangedListener(TextWatcher changeWatch) {
		this.valueWatcher = changeWatch;
	}

	private class NumberInputFilter implements InputFilter {

		private int Max;
		private int Min;

		public NumberInputFilter(int Min, int Max) {
			this.Max = Max;
			this.Min = Min;
		}

		@Override
		public CharSequence filter(CharSequence source, int start, int end,
				Spanned dest, int dstart, int dend) {
			// TODO Auto-generated method stub
			// 对source进行过滤
			String str = new String();
			for (int i = start; i < end; i++) {
				char ch = source.charAt(i);
				if (ch >= 48 && ch <= 57) {
					str += ch;
				}
			}
			CharSequence backChar = str;
			if (source.length() > 0) {
				if (Integer.parseInt(dest.toString() + str.toString()) > Max) {
					backChar = "";
				} else if (Integer
						.parseInt(dest.toString() + str.toString()) < Min) {
					backChar = String.valueOf(Min);
				}
			}
			return backChar;
		}
	}
}